from collections import Counter
from app.common import mongo_client
from settings import mongo_settings


def caculate_accuracy_by_phase(phase):
    """计算期数频率 最多计算 100期
    2 8 定律
    """
    query = {'_id': phase}

    item = mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_prediction].find_one,
        query
    )
    if item:
        return

    item = mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_lottery].find_one,
        query
    )

    result = list(mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_frequency].find,
        {'_id': {'$lt': query['_id']}}
    ).sort([('_id', -1)]).limit(1))

    item_predict = result[0]
    blue = item['blue']
    reds = item['red']

    f_20_red_hot = [i['ball'] for i in item_predict['frequency_20']['red_hot']]
    f_20_red_cold = [i['ball'] for i in item_predict['frequency_20']['red_cold']]
    f_20_blue_hot = [i['ball'] for i in item_predict['frequency_20']['blue_hot']]
    f_20_blue_cold = [i['ball'] for i in item_predict['frequency_20']['blue_cold']]
    f_20_blue_hot_accuracy = 1 if blue in f_20_blue_hot else 0
    f_20_blue_cold_accuracy = 1 if blue in f_20_blue_cold else 0
    f_20_red_hot_accuracy = round(len(set(reds) & set(f_20_red_hot)) / 6, 2)
    f_20_red_cold_accuracy = round(len(set(reds) & set(f_20_red_cold)) / 6, 2)

    f_50_red_hot = [i['ball'] for i in item_predict['frequency_50']['red_hot']]
    f_50_red_cold = [i['ball'] for i in item_predict['frequency_50']['red_cold']]
    f_50_blue_hot = [i['ball'] for i in item_predict['frequency_50']['blue_hot']]
    f_50_blue_cold = [i['ball'] for i in item_predict['frequency_50']['blue_cold']]
    f_50_blue_hot_accuracy = 1 if blue in f_50_blue_hot else 0
    f_50_blue_cold_accuracy = 1 if blue in f_50_blue_cold else 0
    f_50_red_hot_accuracy = round(len(set(reds) & set(f_50_red_hot)) / 6, 2)
    f_50_red_cold_accuracy = round(len(set(reds) & set(f_50_red_cold)) / 6, 2)

    f_80_red_hot = [i['ball'] for i in item_predict['frequency_80']['red_hot']]
    f_80_red_cold = [i['ball'] for i in item_predict['frequency_80']['red_cold']]
    f_80_blue_hot = [i['ball'] for i in item_predict['frequency_80']['blue_hot']]
    f_80_blue_cold = [i['ball'] for i in item_predict['frequency_80']['blue_cold']]
    f_80_blue_hot_accuracy = 1 if blue in f_80_blue_hot else 0
    f_80_blue_cold_accuracy = 1 if blue in f_80_blue_cold else 0
    f_80_red_hot_accuracy = round(len(set(reds) & set(f_80_red_hot)) / 6, 2)
    f_80_red_cold_accuracy = round(len(set(reds) & set(f_80_red_cold)) / 6, 2)

    f_100_red_hot = [i['ball'] for i in item_predict['frequency_100']['red_hot']]
    f_100_red_cold = [i['ball'] for i in item_predict['frequency_100']['red_cold']]
    f_100_blue_hot = [i['ball'] for i in item_predict['frequency_100']['blue_hot']]
    f_100_blue_cold = [i['ball'] for i in item_predict['frequency_100']['blue_cold']]
    f_100_blue_hot_accuracy = 1 if blue in f_100_blue_hot else 0
    f_100_blue_cold_accuracy = 1 if blue in f_100_blue_cold else 0
    f_100_red_hot_accuracy = round(len(set(reds) & set(f_100_red_hot)) / 6, 2)
    f_100_red_cold_accuracy = round(len(set(reds) & set(f_100_red_cold)) / 6, 2)

    data = {
        '_id': phase,
        'frequency_20': {'blue_hot': f_20_blue_hot_accuracy, 'blue_cold': f_20_blue_cold_accuracy,
                         'red_hot': f_20_red_hot_accuracy, 'red_cold': f_20_red_cold_accuracy
                         },
        'frequency_50': {'blue_hot': f_50_blue_hot_accuracy, 'blue_cold': f_50_blue_cold_accuracy,
                         'red_hot': f_50_red_hot_accuracy, 'red_cold': f_50_red_cold_accuracy
                         },
        'frequency_80': {'blue_hot': f_80_blue_hot_accuracy, 'blue_cold': f_80_blue_cold_accuracy,
                         'red_hot': f_80_red_hot_accuracy, 'red_cold': f_80_red_cold_accuracy
                         },
        'frequency_100': {'blue_hot': f_100_blue_hot_accuracy, 'blue_cold': f_100_blue_cold_accuracy,
                          'red_hot': f_100_red_hot_accuracy, 'red_cold': f_100_red_cold_accuracy
                         }
    }
    mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_prediction].insert,
        data)


def caculate_accuracy_latest():
    """计算最新一期的频率"""
    item = list(mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_lottery].find,
        {},
        {'_id': 1}).sort([('_id', -1)]).limit(1))[0]
    phase = item['_id']
    caculate_accuracy_by_phase(phase)
